<?php

namespace app\agent\controller;

use app\common\controller\CommonService;
use think\Db;
use think\facade\Cache;
use think\facade\Validate;

class Login
{


    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login()
    {
        $requestData = request()->post();
        $validate = Validate::make([
            'tel|手机号' => 'require|mobile',
            'password|密码' => 'require|min:6'
        ]);
        if (!$validate->check($requestData)) {
            return CommonService::error($validate->getError());
        }
        $userId = Db::name('user_manage')->where('tel', $requestData['tel'])->value('id');
        $agent = Db::name('agents')->where('uid', $userId)->find();
        if (!$agent) {
            return CommonService::error(1);
        }
        if ($agent['password'] !== md5($requestData['password'])) {
            return CommonService::error(2);
        }
        $responseData['agentToken'] = CommonService::jwtEncode(['id' => $agent['id']], time() + 86400);
        cache('agentAdmin' . $agent['id'], $responseData['agentToken'], 86400);
        return CommonService::success($responseData);
    }


    /**
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function resetPasswordSendSms()
    {
        $requestData = request()->post();
        $validate = Validate::make([
            'tel|手机号' => 'require|mobile',
        ]);
        if (!$validate->check($requestData)) {
            return CommonService::error($validate->getError());
        }
        $userId = Db::name('user_manage')->where('tel', $requestData['tel'])->value('id');
        $agent = Db::name('agents')->where('uid', $userId)->find();
        if (!$agent) {
            return CommonService::error(1);
        }
        if (Cache::get('sendSmsCount' . $requestData['tel']) > 20) {
            return CommonService::error(4);
        }
        $templateParam['code'] = mt_rand(1000, 9999);
        $send = CommonService::sendSms($requestData['tel'], $templateParam, 'SMS_186943888');
        if ($send) {
            if (Cache::get('sendSmsCount' . $requestData['tel']) <= 0) {
                $time = strtotime(date('Y-m-d')) + 86400 - time();
                Cache::set('sendSmsCount' . $requestData['tel'], 1, $time);
            } else {
                Cache::inc('sendSmsCount' . $requestData['tel'], 1);
            }
            Cache::set('smsCode' . $requestData['tel'], $templateParam['code'], 300);
            return CommonService::success('', '验证码已发送！');
        } else {
            return CommonService::error(3);
        }
    }

    /**
     * 重置密码
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function resetPassword()
    {
        $requestData = request()->post();
        $validate = Validate::make([
            'tel|手机号' => 'require|mobile',
            'code|验证码' => 'require',
            'newPassword|新密码' => 'require|min:6',
        ]);
        if (!$validate->check($requestData)) {
            return CommonService::error($validate->getError());
        }
        $userId = Db::name('user_manage')->where('tel', $requestData['tel'])->value('id');
        $agent = Db::name('agents')->where('uid', $userId)->find();
        if (!$agent) {
            return CommonService::error(1);
        }
        if ($requestData['code'] != Cache::get('smsCode' . $requestData['tel'])) {
            return CommonService::error(5);
        }
        Db::name('agents')->where('id', $agent['id'])->update(['password' => md5($requestData['newPassword'])]);
        return CommonService::success('', '密码重置成功！');
    }


}
